import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsBoolean, IsNumber, IsOptional, IsString, ValidateNested } from "class-validator";
import { User } from 'libs/db/entity/user.entity';
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { BaseEntity } from "./base.entity";
import { FeedbackStatusEnum } from './enum/feedback-status.enum';
import { FeedbackTypeEnum } from './enum/feedback-type.enum';
import { Village } from "./village.entity";
import { Villager } from "./villager.entity";
import { file } from "./about/file";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class Feedback extends BaseEntity {

  @ApiProperty({
    description: '反馈主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id ?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(Village)
      }
    ]
  })
  @IsNumber({}, { always: true })
  @ManyToOne(() => Village, Village => Village.feedback, {
    eager: true
  })
  village: Number | Village
  
  @ApiProperty({
    example: 1,
    description: '村民',
    required: true,
    type: Number
  })
  @IsNumber({}, { always: true })
  @ManyToOne(() => Villager, Villager => Villager.feedback, {
    eager: true
  })
  villager: Villager

  @ApiProperty({
    example: false,
    description: '是否匿名',
    required: false
  })
  @IsBoolean()
  @IsOptional()
  @Column({
    default: false
  })
  anonymous: Boolean

  @ApiProperty({
    example: '反馈标题',
    description: '反馈标题',
    required: true,
    type: Number,
    enum: FeedbackTypeEnum
  })
  @IsNumber({},{ always: true })
  @Column({
    type: "enum",
    enum: FeedbackTypeEnum,
  })
  type: FeedbackTypeEnum

  @ApiProperty({
    example: '反馈标题',
    description: '反馈标题',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  title: string

  @ApiProperty({
    example: '反馈内容',
    description: '反馈内容',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "text"
  })
  content: string

  @ApiProperty({
    example: '附属文件',
    description: '附属文件',
    required: false,
    type: file
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  relatedDocuments: file [];

  @ApiProperty({
    example: 0,
    description: '反馈状态',
    required: true,
    type: Number,
    enum: FeedbackStatusEnum
  })
  @IsNumber()
  @IsOptional()
  @Column({
    type: "enum",
    enum: FeedbackStatusEnum,
    default: FeedbackStatusEnum.village
  })
  status: FeedbackStatusEnum

  @ApiProperty({
    example: false,
    description: '是否已处理',
    required: false
  })
  @IsBoolean()
  @IsOptional()
  @Column({
    default: false
  })
  processed: Boolean

  @ApiProperty({
    example: 1,
    description: '反馈处理者',
    required: false,
    type: Number
  })
  @ManyToOne(() => User, User => User.feedbacks)
  @JoinColumn()
  handler ?: Number | User

  @ApiProperty({
    example: '反馈处理结果',
    description: '反馈处理结果',
    required: false
  })
  @IsString()
  @IsOptional()
  @Column({
    type: "text",
    nullable: true,
  })
  handlRes: string

  @ApiProperty({
    example: false,
    description: '处理结果被重置',
    required: false
  })
  @IsBoolean()
  @IsOptional()
  @Column({
    default: false
  })
  reseted: Boolean

  @ApiProperty({
    example: '向上级反馈附带信息',
    description: '向上级反馈附带信息',
    required: false
  })
  @IsString()
  @IsOptional()
  @Column({
    type: "char",
    nullable: true,
    length: 255
  })
  upMessage: string

  @ApiProperty({
    example: '退回反馈附带信息',
    description: '退回反馈附带信息',
    required: false
  })
  @IsString()
  @IsOptional()
  @Column({
    type: "char",
    nullable: true,
    length: 255
  })
  downMessage: string
}